<?php


namespace App\Api\Helpers;

use Symfony\Component\HttpFoundation\Response as HttpCode;

Class ApiResponse
{   const retcode_name = 'retcode';
    const msg_name     = 'msg';
    const data_name    = 'data';

    static protected $response = [];
    static protected $headers = [];

    const retcode = HttpCode::HTTP_OK;

    public function response(){
        return response()->json(self::$response,200,self::$headers,JSON_UNESCAPED_UNICODE);
    }

    static public function success($msg = '', $data = [])
    {
        self::setResponse(self::retcode, $msg, $data);
        return (new self)->response();
    }

    static public function error($code, $msg, $data = [])
    {
        self::setResponse($code, $msg, $data);
        return (new self)->response();
    }

    static public function fill($data)
    {
        self::$response[self::data_name] = array_merge(self::$response[self::data_name], $data);
        return (new self);
    }


    static public function setResponse($code, $msg, $data = [])
    {
        self::$response = [
            self::retcode_name => $code,
            self::msg_name     => $msg,
            self::data_name    => $data
//            "request_id"       => IRequ::getRequestId()
        ];
        return new self();
    }

    static public function withHeaders(Array $headers)
    {
        self::$headers = $headers;
        return new self();
    }

}
